from django.contrib import admin
from django.utils.safestring import mark_safe
from django.core.mail import send_mail

from .models import Articles, Cover, Tags, Category, Comment, Avatars, UserInfo, Site, Feedback, NewsData, Menu, \
    MenuImg, MyInfo


# 文章表admin
@admin.register(Articles)
class ArticlesAdmin(admin.ModelAdmin):
    # 操作字段
    def get_edit_bin(self):
        return mark_safe(f"""<a href='/admin/app/articles/{self.nid}/change'>编辑</a>
            """)

    get_edit_bin.short_description = '操作'

    # 封面展示
    def get_cover(self):
        cover = Cover.objects.get(nid=self.cover_id)
        return mark_safe(f"<img src='/api{cover.url.url}' style='width: 140px;height: 65px'></img>")

    get_cover.short_description = '封面'
    # 显示字典
    list_display = ['title', get_cover, 'user', 'status', 'look_count',
                    'comment_count', 'digg_count', 'collects_count', 'word',
                    'change_date', get_edit_bin]
    # 只读字段
    readonly_fields = ('look_count', 'comment_count', 'digg_count', 'collects_count', 'link', 'word')
    # 搜索字段
    search_fields = ['title', 'category__title', 'user']
    # 筛选字段
    list_filter = ['change_date', 'status', 'category']
    # 排序方法
    ordering = ['-nid']


# 文章封面表admin
@admin.register(Cover)
class CoverAdmin(admin.ModelAdmin):
    def getcover(self):
        cover = Cover.objects.get(nid=self.nid)
        return mark_safe(f"<img src='/api{cover.url.url}' style='width: 140px;height: 65px'></img>")

    getcover.short_description = '封面'

    list_display = [getcover]


# 标签表admin
@admin.register(Tags)
class TagsAdmin(admin.ModelAdmin):
    pass


# 分类表admin
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    pass


# 评论表admin
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    pass


# 头像表admin
@admin.register(Avatars)
class AvatarsAdmin(admin.ModelAdmin):
    def getAvatars(self):
        avatars = Avatars.objects.get(nid=self.nid)
        return mark_safe(f"<img src='{avatars.url.url}' style='width: 100px;height: 80px'></img>")

    getAvatars.short_description = '头像'

    list_display = [getAvatars]


# 用户表admin
@admin.register(UserInfo)
class UserInfoAdmin(admin.ModelAdmin):
    list_display = ['username', 'account_status', 'last_login', 'is_superuser', 'create_time']


# 网站信息表admin
@admin.register(Site)
class SiteAdmin(admin.ModelAdmin):
    list_display = ['title', 'abstract', 'key_words', 'record', 'create_date', 'version', 'icon']


# 分类表admin
@admin.register(Feedback)
class FeedbackAdmin(admin.ModelAdmin):

    def has_add_permission(self, request):
        return False

    def save_model(self, request, obj, form, change):
        # 如果回复了，则发送邮件并保存
        if obj.processing_content:
            send_mail(
                '你反馈的内容被回复了',
                obj.processing_content,
                '1824376609@qq.com',
                [obj.email]
            )
            obj.status = True
        return super(FeedbackAdmin, self).save_model(request, obj, form, change)

    list_display = ['email', 'content', 'status', 'processing_content']
    readonly_fields = ['email', 'content', 'status']


# 新闻数据表admin
@admin.register(NewsData)
class NewsDataAdmin(admin.ModelAdmin):
    def title(self):
        news = NewsData.objects.filter(id=self.id).first()
        return mark_safe(f'<p style="width:300px">{news.title}</p>')

    title.short_description = '标题'

    def link(self):
        news = NewsData.objects.filter(id=self.id).first()
        return mark_safe(f'<a style="width:200px" target="_Blank" href={news.link}>{news.link}</a>')

    link.short_description = '链接'

    list_display = [title, 'type', 'creation_time', link]
    readonly_fields = ['title', 'type', 'creation_time', 'link', 'info']
    list_filter = ['creation_time', 'type']


# 站点背景表admin
@admin.register(Menu)
class MenuAdmin(admin.ModelAdmin):
    pass


# 站点背景图表admin
@admin.register(MenuImg)
class MenuImgAdmin(admin.ModelAdmin):
    pass


# 个人信息表admin
@admin.register(MyInfo)
class MenuImgAdmin(admin.ModelAdmin):
    list_display = ['name', 'email', 'github_url']
